package com.fitness.fitness.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fitness.fitness.convertor.CourseConvertor;
import com.fitness.fitness.mapper.CourseCategoryMapper;
import com.fitness.fitness.mapper.CourseMapper;
import com.fitness.fitness.modal.course.dto.CourseDto;
import com.fitness.fitness.modal.course.entity.Course;
import com.fitness.fitness.modal.course.entity.CourseCategory;
import com.fitness.fitness.modal.course.params.CoursePageQueryParams;
import com.fitness.fitness.modal.course.vo.CourseVO;
import com.fitness.fitness.modal.datavisual.entity.CourseTopNVisual;
import com.fitness.fitness.service.ICourseService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.Map;

@Service
@RequiredArgsConstructor
public class CourseServiceImpl extends ServiceImpl<CourseMapper, Course> implements ICourseService {

    private final CourseConvertor courseConvertor;
    private final CourseCategoryMapper courseCategoryMapper;

    @Override
    public Page<CourseVO> getCoursePage(CoursePageQueryParams queryParams) {
        // 参数构建
        int pageNum = queryParams.getPageNum();
        int pageSize = queryParams.getPageSize();
        Page<Course> page = new Page<>(pageNum, pageSize);

        // 查询数据
        Page<Course> coachBoPage = this.baseMapper.getCoursePage(page, queryParams);

        return courseConvertor.entity2Vo(coachBoPage);
    }

    @Override
    public boolean addCourse(CourseDto courseDto) {
        Long categoryId = courseDto.getCategoryId();
        CourseCategory category = courseCategoryMapper.selectById(categoryId);
        courseDto.setCategoryName(category.getName());
        return save(courseConvertor.dto2Entity(courseDto));
    }

    @Override
    public boolean updateCourse(CourseDto courseDto) {
        return updateById(courseConvertor.dto2Entity(courseDto));
    }

    @Override
    public Map<Long, Course> getCourseMap() {
        return this.baseMapper.getCourseMap();
    }
}
